<?php
namespace App\Http\Controllers\Business\Admin\Order;

use App\Models\Comment;
use App\Models\Order;
use App\Models\OrderRefund;
use App\Models\RedEnvelope;
use App\Models\Recharge;
use App\Models\Refund;
use App\Models\User;
use App\Models\Shop;
use App\Models\City;
use App\Models\AdminUser;
use Illuminate\Support\Facades\Auth;

class OrderManagement
{
    //充值列表
    public static function rechargeList($starttime = '', $lasttime = '',  $search = '', $perpage = 50) {
        if (!$starttime || !$lasttime) {
            $starttime = '2016-01-01 00:00:00';
            $lasttime = date('Y-m-d H:i:s', time());
        }
        if ($search == '') {
            $recharge = Recharge::where('recharges.created_at', '>', $starttime)
                ->where('recharges.created_at', '<', $lasttime)
                ->join('users', 'recharges.userid', '=', 'users.id')
                ->select('recharges.id as rechargeid', 'recharges.userid', 'users.mobile', 'users.nickname', 'recharges.rechargenumber', 'recharges.fee', 'recharges.actualmoney','recharges.path', 'recharges.money', 'recharges.doublestatus', 'recharges.created_at')
                ->orderBy('recharges.created_at', 'desc')
                ->paginate($perpage);
        } else {
            $GLOBALS['search'] = $search;
            $recharge = Recharge::where('recharges.created_at', '>', $starttime)
                ->where('recharges.created_at', '<', $lasttime)
                ->where(function($query) {
                    $query->orWhere('recharges.path', 'like', '%'.$GLOBALS['search'].'%')
                        ->orWhere('recharges.rechargenumber', $GLOBALS['search'])
                        ->orWhere('recharges.doublestatus', 'like', '%'.$GLOBALS['search'].'%')
                        ->orWhere('users.mobile', $GLOBALS['search'])
                        ->orWhere('users.nickname', 'like', '%' . $GLOBALS['search'] .'%');
                })
                ->join('users', 'recharges.userid', '=', 'users.id')
                ->select('recharges.id as rechargeid', 'recharges.userid', 'users.mobile', 'users.nickname', 'recharges.rechargenumber', 'recharges.fee', 'recharges.actualmoney','recharges.path', 'recharges.money', 'recharges.doublestatus', 'recharges.created_at')
                ->orderBy('recharges.created_at', 'desc')
                ->paginate($perpage);
        }
        return ['status' => 'success', 'data' => $recharge];
    }
    //某个时间段获取充值统计数据，充值笔数，充值金额，服务费，实付金额
    public static function getRechargeStatistical($startTime, $lastTime, $search='') {
        if(!$startTime||!$lastTime) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        if($search == '') {
            //没有搜搜内容
            $count = Recharge::where('created_at', '>=', $startTime)->where('created_at', '<=', $lastTime)->count();
            $moneyTotal = Recharge::where('created_at', '>=', $startTime)->where('created_at', '<=', $lastTime)->sum('money');
            $feeTotal = Recharge::where('created_at', '>=', $startTime)->where('created_at', '<=', $lastTime)->sum('fee');
            $actualMoneyTotal = Recharge::where('created_at', '>=', $startTime)->where('created_at', '<=', $lastTime)->sum('actualmoney');
        } else {
            $GLOBALS['search'] = $search;
            $count = Recharge::where('recharges.created_at', '>=', $startTime)->where('recharges.created_at', '<=', $lastTime)
                ->where(function($query) {
                    $query->orWhere('recharges.path', 'like', '%'.$GLOBALS['search'].'%')
                        ->orWhere('recharges.rechargenumber', $GLOBALS['search'])
                        ->orWhere('recharges.doublestatus', 'like', '%'.$GLOBALS['search'].'%')
                        ->orWhere('users.mobile', $GLOBALS['search'])
                        ->orWhere('users.nickname', 'like', '%' . $GLOBALS['search'] .'%');
                })
                ->join('users', 'recharges.userid', '=', 'users.id')
                ->count();
            $moneyTotal = Recharge::where('recharges.created_at', '>=', $startTime)->where('recharges.created_at', '<=', $lastTime)
                ->where(function($query) {
                    $query->orWhere('recharges.path', 'like', '%'.$GLOBALS['search'].'%')
                        ->orWhere('recharges.rechargenumber', $GLOBALS['search'])
                        ->orWhere('recharges.doublestatus', 'like', '%'.$GLOBALS['search'].'%')
                        ->orWhere('users.mobile', $GLOBALS['search'])
                        ->orWhere('users.nickname', 'like', '%' . $GLOBALS['search'] .'%');
                })
                ->join('users', 'recharges.userid', '=', 'users.id')
                ->sum('recharges.money');
            $feeTotal = Recharge::where('recharges.created_at', '>=', $startTime)->where('recharges.created_at', '<=', $lastTime)
                ->where(function($query) {
                    $query->orWhere('recharges.path', 'like', '%'.$GLOBALS['search'].'%')
                        ->orWhere('recharges.rechargenumber', $GLOBALS['search'])
                        ->orWhere('recharges.doublestatus', 'like', '%'.$GLOBALS['search'].'%')
                        ->orWhere('users.mobile', $GLOBALS['search'])
                        ->orWhere('users.nickname', 'like', '%' . $GLOBALS['search'] .'%');
                })
                ->join('users', 'recharges.userid', '=', 'users.id')
                ->sum('recharges.fee');
            $actualMoneyTotal = Recharge::where('recharges.created_at', '>=', $startTime)->where('recharges.created_at', '<=', $lastTime)
                ->where(function($query) {
                    $query->orWhere('recharges.path', 'like', '%'.$GLOBALS['search'].'%')
                        ->orWhere('recharges.rechargenumber', $GLOBALS['search'])
                        ->orWhere('recharges.doublestatus', 'like', '%'.$GLOBALS['search'].'%')
                        ->orWhere('users.mobile', $GLOBALS['search'])
                        ->orWhere('users.nickname', 'like', '%' . $GLOBALS['search'] .'%');
                })
                ->join('users', 'recharges.userid', '=', 'users.id')
                ->sum('recharges.actualmoney');
        }
        $data = [
            'count' => $count,
            'moneytotal' => $moneyTotal,
            'feetotal' => $feeTotal,
            'actualmoneytotal' => $actualMoneyTotal
        ];
        return ['status'=>'success', 'data'=>$data];
    }
    //导出充值信息
    public static function exportRechargeList($starttime, $lasttime) {
        if(!$starttime|| !$lasttime) {
            return ['status'=>'failed', 'message'=>'参数不对'];
        }
        $data = Recharge::where('recharges.status', '1')
            ->where('recharges.created_at', '>=', $starttime)
            ->where('recharges.created_at', '<=', $lasttime)
            ->join('users', 'recharges.userid', '=', 'users.id')
            ->select('recharges.id as rechargeid', 'users.mobile', 'users.nickname', 'recharges.rechargenumber', 'recharges.path', 'recharges.money','recharges.fee','recharges.actualmoney', 'recharges.doublestatus', 'recharges.created_at')
            ->get();
        return ['status'=>'success', 'data'=>$data];
    }
    //更新充值状态
    public static function updateRechargeDoubleStatus($userId, $rechargeNumber) {
        if(!$userId || !$rechargeNumber) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        Recharge::where('rechargenumber', $rechargeNumber)->where('userid', $userId)->update(['doubletime'=>date('Y-m-d H:i:s')]);
        Recharge::doubleModel($userId);
        return ['status'=>'success', 'message'=>'修改成功'];
    }
    //充值详情
    public static function rechargeDetails($rechargeid) {
        if (!$rechargeid) {
            return ['status' => 'failed', 'message' => '请求数据不完整！'];
        }
        $path = Recharge::where('id', $rechargeid)->pluck('path')->first();
        if($path != "后台充值") {
            $recharge = Recharge::where('recharges.id', $rechargeid)
                ->join('users', 'recharges.userid', '=', 'users.id')
                ->select('recharges.id as rechargeid', 'users.mobile', 'users.nickname', 'recharges.rechargenumber', 'recharges.path', 'recharges.money', 'recharges.fee', 'recharges.actualmoney','recharges.doublemoney', 'recharges.doublestatus', 'recharges.created_at', 'recharges.doubletime', 'recharges.doublemoney', 'recharges.isdoublecoupons', 'recharges.sharestatus')
                ->first();
        } else {
            $recharge = Recharge::where('recharges.id', $rechargeid)
                ->join('users', 'recharges.userid', '=', 'users.id')
                ->join('admin_users', 'recharges.adminid', '=', 'admin_users.id')
                ->select('recharges.id as rechargeid', 'users.mobile', 'users.nickname', 'recharges.rechargenumber', 'recharges.path', 'recharges.money', 'recharges.fee', 'recharges.actualmoney','recharges.doublemoney', 'recharges.doublestatus', 'recharges.created_at', 'recharges.doubletime', 'recharges.doublemoney', 'recharges.isdoublecoupons','recharges.sharestatus','recharges.note','admin_users.realname as adminname', 'admin_users.mobile as adminmobile')
                ->first();
        }
        return ['status' => 'success', 'data' => $recharge];
    }
    //订单列表
    public static function orderList($cityid, $starttime = '', $lasttime = '', $search = '', $perpage = 50) {
        if(!$cityid) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        if (!$starttime || !$lasttime) {
            $starttime = '2016-01-01 00:00:00';
            $lasttime = date('Y-m-d H:i:s', time());
        }
        if($cityid == 1) {
            //南昌总部
            if ($search != '') {
                $GLOBALS['search'] = $search;
                $order = Order::where('orders.created_at', '>=', $starttime)
                    ->where('orders.created_at', '<=', $lasttime)
                    ->where('shops.cityid', $cityid)
                    ->join('users', 'orders.userid', '=', 'users.id')
                    ->join('shops', 'orders.shopid', '=', 'shops.id')
                    ->leftjoin('admin_users', 'orders.maintainerid', '=', 'admin_users.id')
                    ->where(function ($query) {
                        $query->orWhere('users.mobile', $GLOBALS['search'])
                            ->orWhere('users.nickname', 'like', '%' . $GLOBALS['search'] . '%')
                            ->orWhere('orders.shopname', 'like', '%' . $GLOBALS['search'] . '%')
                            ->orWhere('orders.ordernumber', $GLOBALS['search'])
                            ->orWhere('shops.code', $GLOBALS['search'])
                            ->orWhere('shops.firstclassname', $GLOBALS['search'])
                            ->orWhere('shops.secondclassname', $GLOBALS['search']);
                    })
                    ->orderBy('orders.created_at', 'desc')
                    ->select('orders.id as orderid', 'orders.ordernumber','orders.path', 'orders.refundstatus','shops.code', 'shops.firstclassname',
                        'shops.secondclassname', 'shops.oldmaintainer', 'users.id as userid','users.mobile', 'users.nickname', 'orders.shopname','orders.orderamount',
                        'orders.actualamount','orders.redenvelopeamount','orders.bonus', 'orders.created_at', 'admin_users.realname as adminname')
                    ->paginate($perpage);
            } else {
                $order = Order::where('orders.created_at', '>=', $starttime)
                    ->where('orders.created_at', '<=', $lasttime)
                    ->where('shops.cityid', $cityid)
                    ->join('users', 'orders.userid', '=', 'users.id')
                    ->join('shops', 'orders.shopid', '=', 'shops.id')
                    ->join('admin_users', 'orders.maintainerid', '=', 'admin_users.id')
                    ->orderBy('orders.created_at', 'desc')
                    ->select('orders.id as orderid', 'orders.ordernumber','orders.path', 'orders.refundstatus','shops.code', 'shops.firstclassname',
                        'shops.secondclassname', 'shops.oldmaintainer', 'users.id as userid','users.mobile', 'users.nickname', 'orders.shopname','orders.orderamount',
                        'orders.actualamount','orders.redenvelopeamount','orders.bonus', 'orders.created_at', 'admin_users.realname as adminname')
                    ->paginate($perpage);
            }
        } else {
            //不在南昌的分部
            if ($search != '') {
                $GLOBALS['search'] = $search;
                $order = Order::where('orders.created_at', '>=', $starttime)
                    ->where('orders.created_at', '<=', $lasttime)
                    ->where('shops.cityid', $cityid)
                    ->join('users', 'orders.userid', '=', 'users.id')
                    ->join('shops', 'orders.shopid', '=', 'shops.id')
                    ->join('admin_users', 'orders.maintainerid', '=', 'admin_users.id')
                    ->where(function ($query) {
                        $query->orWhere('users.mobile', $GLOBALS['search'])
                            ->orWhere('users.nickname', 'like', '%' . $GLOBALS['search'] . '%')
                            ->orWhere('orders.shopname', 'like', '%' . $GLOBALS['search'] . '%')
                            ->orWhere('orders.ordernumber', $GLOBALS['search'])
                            ->orWhere('shops.code', $GLOBALS['search'])
                            ->orWhere('shops.firstclassname', $GLOBALS['search'])
                            ->orWhere('shops.secondclassname', $GLOBALS['search']);
                    })
                    ->orderBy('orders.created_at', 'desc')
                    ->select('orders.id as orderid', 'orders.ordernumber','orders.path', 'orders.refundstatus','shops.code', 'shops.firstclassname',
                        'shops.secondclassname', 'shops.oldmaintainer', 'users.id as userid','users.mobile', 'users.nickname', 'orders.shopname','orders.orderamount',
                        'orders.actualamount','orders.redenvelopeamount','orders.bonus', 'orders.created_at', 'admin_users.realname as adminname')
                    ->paginate($perpage);
            } else {
                $order = Order::where('orders.created_at', '>=', $starttime)
                    ->where('orders.created_at', '<=', $lasttime)
                    ->where('shops.cityid', $cityid)
                    ->join('users', 'orders.userid', '=', 'users.id')
                    ->join('shops', 'orders.shopid', '=', 'shops.id')
                    ->join('admin_users', 'orders.maintainerid', '=', 'admin_users.id')
                    ->orderBy('orders.created_at', 'desc')
                    ->select('orders.id as orderid', 'orders.ordernumber','orders.path', 'orders.refundstatus','shops.code', 'shops.firstclassname',
                        'shops.secondclassname', 'shops.oldmaintainer', 'users.id as userid','users.mobile', 'users.nickname', 'orders.shopname','orders.orderamount',
                        'orders.actualamount','orders.redenvelopeamount','orders.bonus', 'orders.created_at', 'admin_users.realname as adminname')
                    ->paginate($perpage);
            }
            foreach($order as $k=>$v) {
                $order[$k]['mobile'] = str_replace(substr($order[$k]['mobile'], 3, 4), '****', $order[$k]['mobile']);
            }
        }
        return ['status' => 'success', 'data' => $order];
    }
    //某个时间段获取统计数据， 订单总数，扫描方式， 消费金额，红包金额， 实付金额， 获取红包
    public static function getOrderStatistical($cityId, $startTime, $lastTime, $search='') {
        if(!$cityId||!$startTime||!$lastTime) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        if($search == '') {
            //没有搜索内容
            $total = Order::where('cityid', $cityId)->where('created_at', '>=', $startTime)
                ->where('created_at', '<=', $lastTime)->where('refundstatus', 0)->count();
            $userSweepCount = Order::where('cityid', $cityId)->where('created_at', '>=', $startTime)
                ->where('created_at', '<=', $lastTime)->where('path', 'user')->where('refundstatus', 0)->count();
            $shopSweepCount = Order::where('cityid', $cityId)->where('created_at', '>=', $startTime)
                ->where('created_at', '<=', $lastTime)->where('path', 'shop')->where('refundstatus', 0)->count();
            $sum = Order::where('cityid', $cityId)->where('created_at', '>=', $startTime)
                ->where('created_at', '<=', $lastTime)->where('refundstatus', 0)->sum('orderamount');
            $redEnvelopeAmount = Order::where('cityid', $cityId)->where('created_at', '>=', $startTime)
                ->where('created_at', '<=', $lastTime)->where('refundstatus', 0)->sum('redenvelopeamount');
            $actualAmount = Order::where('cityid', $cityId)->where('created_at', '>=', $startTime)
                ->where('created_at', '<=', $lastTime)->where('refundstatus', 0)->sum('actualamount');
            $redEnvelopeBonus = Order::where('cityid', $cityId)->where('created_at', '>=', $startTime)
                ->where('created_at', '<=', $lastTime)->where('refundstatus', 0)->sum('bonus');
        } else {
            $GLOBALS['search'] = $search;
            $total = Order::where('orders.cityid', $cityId)->where('orders.created_at', '>=', $startTime)
                ->where('orders.created_at', '<=', $lastTime)->where('orders.refundstatus', 0)
                ->join('users', 'orders.userid', '=', 'users.id')
                ->join('shops', 'orders.shopid', '=', 'shops.id')
                ->where(function ($query) {
                    $query->orWhere('users.mobile', $GLOBALS['search'])
                        ->orWhere('users.nickname', 'like', '%' . $GLOBALS['search'] . '%')
                        ->orWhere('orders.shopname', 'like', '%' . $GLOBALS['search'] . '%')
                        ->orWhere('orders.ordernumber', $GLOBALS['search'])
                        ->orWhere('shops.code', $GLOBALS['search'])
                        ->orWhere('shops.firstclassname', $GLOBALS['search'])
                        ->orWhere('shops.secondclassname', $GLOBALS['search']);
                })->count();
            $userSweepCount = Order::where('orders.cityid', $cityId)->where('orders.created_at', '>=', $startTime)
                ->where('orders.created_at', '<=', $lastTime)->where('orders.path', 'user')->where('orders.refundstatus', 0)
                ->join('users', 'orders.userid', '=', 'users.id')
                ->join('shops', 'orders.shopid', '=', 'shops.id')
                ->where(function ($query) {
                    $query->orWhere('users.mobile', $GLOBALS['search'])
                        ->orWhere('users.nickname', 'like', '%' . $GLOBALS['search'] . '%')
                        ->orWhere('orders.shopname', 'like', '%' . $GLOBALS['search'] . '%')
                        ->orWhere('orders.ordernumber', $GLOBALS['search'])
                        ->orWhere('shops.code', $GLOBALS['search'])
                        ->orWhere('shops.firstclassname', $GLOBALS['search'])
                        ->orWhere('shops.secondclassname', $GLOBALS['search']);
                })
                ->count();
            $shopSweepCount = Order::where('orders.cityid', $cityId)->where('orders.created_at', '>=', $startTime)
                ->where('orders.created_at', '<=', $lastTime)->where('orders.path', 'shop')->where('orders.refundstatus', 0)
                ->join('users', 'orders.userid', '=', 'users.id')
                ->join('shops', 'orders.shopid', '=', 'shops.id')
                ->where(function ($query) {
                    $query->orWhere('users.mobile', $GLOBALS['search'])
                        ->orWhere('users.nickname', 'like', '%' . $GLOBALS['search'] . '%')
                        ->orWhere('orders.shopname', 'like', '%' . $GLOBALS['search'] . '%')
                        ->orWhere('orders.ordernumber', $GLOBALS['search'])
                        ->orWhere('shops.code', $GLOBALS['search'])
                        ->orWhere('shops.firstclassname', $GLOBALS['search'])
                        ->orWhere('shops.secondclassname', $GLOBALS['search']);
                })
                ->count();
            $sum = Order::where('orders.cityid', $cityId)->where('orders.created_at', '>=', $startTime)
                ->where('orders.created_at', '<=', $lastTime)->where('orders.refundstatus', 0)
                ->join('users', 'orders.userid', '=', 'users.id')
                ->join('shops', 'orders.shopid', '=', 'shops.id')
                ->where(function ($query) {
                    $query->orWhere('users.mobile', $GLOBALS['search'])
                        ->orWhere('users.nickname', 'like', '%' . $GLOBALS['search'] . '%')
                        ->orWhere('orders.shopname', 'like', '%' . $GLOBALS['search'] . '%')
                        ->orWhere('orders.ordernumber', $GLOBALS['search'])
                        ->orWhere('shops.code', $GLOBALS['search'])
                        ->orWhere('shops.firstclassname', $GLOBALS['search'])
                        ->orWhere('shops.secondclassname', $GLOBALS['search']);
                })
                ->sum('orders.orderamount');
            $redEnvelopeAmount = Order::where('orders.cityid', $cityId)->where('orders.created_at', '>=', $startTime)
                ->where('orders.created_at', '<=', $lastTime)->where('orders.refundstatus', 0)
                ->join('users', 'orders.userid', '=', 'users.id')
                ->join('shops', 'orders.shopid', '=', 'shops.id')
                ->where(function ($query) {
                    $query->orWhere('users.mobile', $GLOBALS['search'])
                        ->orWhere('users.nickname', 'like', '%' . $GLOBALS['search'] . '%')
                        ->orWhere('orders.shopname', 'like', '%' . $GLOBALS['search'] . '%')
                        ->orWhere('orders.ordernumber', $GLOBALS['search'])
                        ->orWhere('shops.code', $GLOBALS['search'])
                        ->orWhere('shops.firstclassname', $GLOBALS['search'])
                        ->orWhere('shops.secondclassname', $GLOBALS['search']);
                })
                ->sum('orders.redenvelopeamount');
            $actualAmount = Order::where('orders.cityid', $cityId)->where('orders.created_at', '>=', $startTime)
                ->where('orders.created_at', '<=', $lastTime)->where('orders.refundstatus', 0)
                ->join('users', 'orders.userid', '=', 'users.id')
                ->join('shops', 'orders.shopid', '=', 'shops.id')
                ->where(function ($query) {
                    $query->orWhere('users.mobile', $GLOBALS['search'])
                        ->orWhere('users.nickname', 'like', '%' . $GLOBALS['search'] . '%')
                        ->orWhere('orders.shopname', 'like', '%' . $GLOBALS['search'] . '%')
                        ->orWhere('orders.ordernumber', $GLOBALS['search'])
                        ->orWhere('shops.code', $GLOBALS['search'])
                        ->orWhere('shops.firstclassname', $GLOBALS['search'])
                        ->orWhere('shops.secondclassname', $GLOBALS['search']);
                })
                ->sum('orders.actualamount');
            $redEnvelopeBonus = Order::where('orders.cityid', $cityId)->where('orders.created_at', '>=', $startTime)
                ->where('orders.created_at', '<=', $lastTime)->where('orders.refundstatus', 0)
                ->join('users', 'orders.userid', '=', 'users.id')
                ->join('shops', 'orders.shopid', '=', 'shops.id')
                ->where(function ($query) {
                    $query->orWhere('users.mobile', $GLOBALS['search'])
                        ->orWhere('users.nickname', 'like', '%' . $GLOBALS['search'] . '%')
                        ->orWhere('orders.shopname', 'like', '%' . $GLOBALS['search'] . '%')
                        ->orWhere('orders.ordernumber', $GLOBALS['search'])
                        ->orWhere('shops.code', $GLOBALS['search'])
                        ->orWhere('shops.firstclassname', $GLOBALS['search'])
                        ->orWhere('shops.secondclassname', $GLOBALS['search']);
                })
                ->sum('orders.bonus');
        }
        $data = [
            'total' => $total,
            'usersweepcount' => $userSweepCount,
            'shopsweepcount' => $shopSweepCount,
            'sum' => $sum,
            'redenvelopeamount' => $redEnvelopeAmount,
            'actualamount' => $actualAmount,
            'redenvelopebonus' => $redEnvelopeBonus
        ];
        return ['status'=>'success', 'data'=>$data];
    }
    //导出订单
    public static function exportOrderList($cityId, $startime, $lasttime, $search='') {
        if(!$startime||!$lasttime||!$cityId) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        if($search) {
            $GLOBALS['search'] = $search;
            $data = Order::where('orders.created_at', '>=', $startime)
                ->where('orders.created_at', '<=', $lasttime)
                ->where('orders.cityid', $cityId)
                ->join('users', 'orders.userid', '=', 'users.id')
                ->join('shops', 'orders.shopid', '=', 'shops.id')
                ->join('admin_users', 'orders.maintainerid', '=', 'admin_users.id')
                ->where(function($query) {
                    $query->orwhere('shops.code', $GLOBALS['search'])
                        ->orwhere('shops.name', $GLOBALS['search']);
                })
                ->select('orders.id as orderid', 'orders.ordernumber','orders.refundstatus', 'shops.code', 'shops.firstclassname', 'shops.secondclassname', 'shops.oldmaintainer', 'users.mobile', 'users.nickname', 'orders.shopname', 'orders.actualamount', 'orders.redenvelopeamount','orders.orderamount','orders.accountamount','orders.created_at', 'admin_users.realname as adminname')
                ->get();
        } else {
            $data = Order::where('orders.created_at', '>=', $startime)
                ->where('orders.created_at', '<=', $lasttime)
                ->where('orders.cityid', $cityId)
                ->join('users', 'orders.userid', '=', 'users.id')
                ->join('shops', 'orders.shopid', '=', 'shops.id')
                ->join('admin_users', 'orders.maintainerid', '=', 'admin_users.id')
                ->select('orders.id as orderid', 'orders.ordernumber','orders.refundstatus', 'shops.code', 'shops.firstclassname', 'shops.secondclassname', 'shops.oldmaintainer', 'users.mobile', 'users.nickname', 'orders.shopname', 'orders.actualamount', 'orders.redenvelopeamount','orders.orderamount','orders.accountamount','orders.created_at', 'admin_users.realname as adminname')
                ->get();
        }
        return ['status'=>'success', 'data'=>$data];
    }
    //订单详情
    public static function orderDetails($orderid) {
        if (!$orderid) {
            return ['status' => 'failed', 'message' => '请求数据不完整！'];
        }
        $order = Order::where('orders.id', $orderid)->join('users', 'orders.userid', '=', 'users.id')
            ->join('shops', 'shops.id', '=', 'orders.shopid')
            ->select('orders.id as orderid', 'orders.ordernumber', 'users.mobile', 'users.nickname', 'orders.shopname', 'orders.actualamount',
                'orders.accountamount', 'orders.created_at', 'shops.cityid','shops.contactnumber', 'shops.firstclassname','shops.secondclassname',
                'shops.address', 'orders.orderamount', 'orders.shopamount', 'orders.redenvelopeamount', 'orders.hqamount', 'orders.agentamount',
                'orders.bonus', 'orders.fee', 'orders.refundstatus', 'orders.redenvelopeid', 'orders.bonusid'
            )
            ->first();
        if(!$order) {
            return ['status'=>'failed', 'message'=>'获取信息失败'];
        }
        $order['redenveoplecityname'] = City::where('id', $order['cityid'])->pluck('name')->first();
        if($order['cityid'] != 1) {
            $order['mobile'] = str_replace(substr($order['mobile'], 3, 4), '****', $order['mobile']);
        }
        $userRedEnvelope = [];
        if($order['redenvelopeid']) {
            $userRedEnvelope = RedEnvelope::where('id', abs($order['redenvelopeid']))->select('hqamount','agentamount','cityid', 'endtime')->first();
            if($userRedEnvelope) {
                $userRedEnvelope['cityname'] = City::where('id', $userRedEnvelope['cityid'])->pluck('name')->first();
            }
        }
        $order['userredenvelope'] = $userRedEnvelope;
        $sendRedEnvelope = [];
        if($order['bonusid']) {
            $sendRedEnvelope = RedEnvelope::where('id', $order['bonusid'])->select('hqamount','agentamount','cityid', 'endtime')->first();
            if($sendRedEnvelope) {
                $sendRedEnvelope['cityname'] = City::where('id', $sendRedEnvelope['cityid'])->pluck('name')->first();
            }
        }
        $order['sendredenvelope'] = $sendRedEnvelope;

        if($order['refundstatus']) {
            $order['refundinfo'] = OrderRefund::where('order_refunds.ordernumber', $order['ordernumber'])
                ->join('admin_users', 'order_refunds.adminid', '=', 'admin_users.id')
                ->select('order_refunds.money', 'order_refunds.created_at', 'order_refunds.redenvelopeinfo', 'order_refunds.remark', 'admin_users.realname as adminname', 'admin_users.mobile as adminmobile')
                ->first();
        } else {
            $order['refundinfo'] = [
                'money' => 0,
                'created_at' => '0000-00-00 00:00:00',
                'redenvelopeinfo' => '',
                'remark' => '',
                'adminname' => '' ,
                'adminmobile' => '',
            ];
        }
        return ['status' => 'success', 'data' => $order];
    }
    //评论列表
    public static function commentList($cityid, $starttime = '', $lasttime = '', $search = '', $perpage = 50) {
        if(!$cityid) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        if (!$starttime || !$lasttime) {
            $starttime = '2016-01-01 00:00:00';
            $lasttime = date('Y-m-d H:i:s', time());
        }

        if ($search != '') {
            $GLOBALS['search'] = $search;
            $comment = Comment::where('comments.created_at', '>', $starttime)
                ->where('comments.created_at', '<', $lasttime)->where('comments.isdel', '0')
                ->where('shops.cityid', $cityid)
                ->join('users', 'users.id', '=', 'comments.userid')
                ->join('shops', 'shops.id', '=', 'comments.shopid')
                ->where(function ($query) {
                    $query->orWhere('comments.ordernumber', $GLOBALS['search'])
                        ->orWhere('users.nickname', 'like', '%'.$GLOBALS['search'].'%')
                        ->orWhere('shops.name', 'like', '%'.$GLOBALS['search'].'%')
                        ->orWhere('users.mobile', $GLOBALS['search']);
                })
                ->select('comments.id as commentid', 'comments.ordernumber', 'users.mobile', 'users.nickname', 'shops.name as shopname', 'comments.created_at', 'comments.score', 'comments.content', 'comments.picture')
                ->orderBy('comments.created_at', 'desc')
                ->paginate($perpage);
        } else {
            $comment = Comment::where('comments.created_at', '>', $starttime)
                ->where('comments.created_at', '<', $lasttime)->where('comments.isdel', '0')
                ->where('shops.cityid', $cityid)
                ->join('users', 'users.id', '=', 'comments.userid')
                ->join('shops', 'shops.id', '=', 'comments.shopid')
                ->select('comments.id as commentid', 'comments.ordernumber', 'users.mobile', 'users.nickname', 'shops.name as shopname', 'comments.created_at', 'comments.score', 'comments.content', 'comments.picture')
                ->orderBy('comments.created_at', 'desc')
                ->paginate($perpage);
        }
        foreach($comment as $k=>$v) {
            if($comment[$k]['picture'] == '') {
                $comment[$k]['picturearray'] = [];
            } else {
                $comment[$k]['picturearray'] = explode(',', $comment[$k]['picture']);
            }
        }
        return ['status' => 'success', 'data' => $comment];
    }
    //举报列表
    public static function reportList($startTime, $lastTime, $search = '', $skip=0, $take = 50) {
        if (!$startTime || !$lastTime) {
            $startTime = '2016-01-01 00:00:00';
            $lastTime = date('Y-m-d H:i:s', time());
        }
        if ($search != '') {
            $GLOBALS['search'] = $search;
            $lists = Comment::where('comments.created_at', '>', $startTime)->where('comments.created_at', '<', $lastTime)
                ->where('comments.reportstatus', '<>', 0)
                ->join('users', 'users.id', '=', 'comments.userid')
                ->join('shops', 'shops.id', '=', 'comments.shopid')
                ->where(function ($query) {
                    $query->orWhere('comments.ordernumber', $GLOBALS['search'])
                        ->orWhere('users.nickname', 'like', '%'.$GLOBALS['search'].'%')
                        ->orWhere('users.mobile', $GLOBALS['search'])
                        ->orWhere('shops.name', 'like', '%'.$GLOBALS['search']. '%');
                })
                ->select('comments.id as commentid', 'comments.ordernumber', 'users.mobile', 'users.nickname', 'shops.name as shopname',
                    'comments.created_at', 'comments.updated_at', 'comments.score', 'comments.content', 'comments.picture', 'comments.reportstatus',
                    'comments.reportcontent', 'comments.reporttime', 'comments.adminid')
                ->orderBy('comments.created_at', 'desc')
                ->skip($skip)->take($take)->get();
            $total = Comment::where('comments.created_at', '>', $startTime)->where('comments.created_at', '<', $lastTime)
                ->where('comments.reportstatus', '<>', 0)
                ->join('users', 'users.id', '=', 'comments.userid')
                ->join('shops', 'shops.id', '=', 'comments.shopid')
                ->where(function ($query) {
                    $query->orWhere('comments.ordernumber', $GLOBALS['search'])
                        ->orWhere('users.nickname', 'like', '%'.$GLOBALS['search'].'%')
                        ->orWhere('users.mobile', $GLOBALS['search'])
                        ->orWhere('shops.name', 'like', '%'.$GLOBALS['search']. '%');
                })->count();
            $waitTotal = Comment::where('comments.created_at', '>', $startTime)->where('comments.created_at', '<', $lastTime)
                ->where('comments.reportstatus', 1)
                ->join('users', 'users.id', '=', 'comments.userid')
                ->join('shops', 'shops.id', '=', 'comments.shopid')
                ->where(function ($query) {
                    $query->orWhere('comments.ordernumber', $GLOBALS['search'])
                        ->orWhere('users.nickname', 'like', '%'.$GLOBALS['search'].'%')
                        ->orWhere('users.mobile', $GLOBALS['search'])
                        ->orWhere('shops.name', 'like', '%'.$GLOBALS['search']. '%');
                })->count();
            $reportInvalidTotal = Comment::where('comments.created_at', '>', $startTime)->where('comments.created_at', '<', $lastTime)
                ->where('comments.reportstatus', 2)
                ->join('users', 'users.id', '=', 'comments.userid')
                ->join('shops', 'shops.id', '=', 'comments.shopid')
                ->where(function ($query) {
                    $query->orWhere('comments.ordernumber', $GLOBALS['search'])
                        ->orWhere('users.nickname', 'like', '%'.$GLOBALS['search'].'%')
                        ->orWhere('users.mobile', $GLOBALS['search'])
                        ->orWhere('shops.name', 'like', '%'.$GLOBALS['search']. '%');
                })->count();
            $deleteTotal = Comment::where('comments.created_at', '>', $startTime)->where('comments.created_at', '<', $lastTime)
                ->where('comments.reportstatus', 3)
                ->join('users', 'users.id', '=', 'comments.userid')
                ->join('shops', 'shops.id', '=', 'comments.shopid')
                ->where(function ($query) {
                    $query->orWhere('comments.ordernumber', $GLOBALS['search'])
                        ->orWhere('users.nickname', 'like', '%'.$GLOBALS['search'].'%')
                        ->orWhere('users.mobile', $GLOBALS['search'])
                        ->orWhere('shops.name', 'like', '%'.$GLOBALS['search']. '%');
                })->count();
        } else {
            $lists = Comment::where('comments.created_at', '>', $startTime)->where('comments.created_at', '<', $lastTime)
                ->where('comments.reportstatus', '<>', 0)
                ->join('users', 'users.id', '=', 'comments.userid')
                ->join('shops', 'shops.id', '=', 'comments.shopid')
                ->select('comments.id as commentid', 'comments.ordernumber', 'users.mobile', 'users.nickname', 'shops.name as shopname',
                    'comments.created_at', 'comments.updated_at', 'comments.score', 'comments.content', 'comments.picture', 'comments.reportstatus',
                    'comments.reportcontent', 'comments.reporttime', 'comments.adminid')
                ->orderBy('comments.created_at', 'desc')
                ->skip($skip)->take($take)->get();
            $total = Comment::where('comments.created_at', '>', $startTime)->where('comments.created_at', '<', $lastTime)
                ->where('comments.reportstatus', '<>', 0)->count();
            $waitTotal = Comment::where('comments.created_at', '>', $startTime)->where('comments.created_at', '<', $lastTime)
                ->where('comments.reportstatus', 1)->count();
            $reportInvalidTotal = Comment::where('comments.created_at', '>', $startTime)->where('comments.created_at', '<', $lastTime)
                ->where('comments.reportstatus', 2)->count();
            $deleteTotal = Comment::where('comments.created_at', '>', $startTime)->where('comments.created_at', '<', $lastTime)
                ->where('comments.reportstatus', 3)->count();
        }
        foreach($lists as $k => $v) {
            if($lists[$k]['adminid'] == 0) {
                $lists[$k]['adminname'] = '';
            } else {
                $lists[$k]['adminname'] = AdminUser::where('id', $lists[$k]['adminid'])->pluck('realname')->first();
            }
        }
        $data = [
            'lists' => $lists,
            'total' => $total,
            'waittotal' => $waitTotal,
            'reportinvalidtotal' => $reportInvalidTotal,
            'deletetotal' => $deleteTotal,
            'starttime' => date('Y-m-d', strtotime($startTime)),
            'lasttime' => date('Y-m-d', strtotime($lastTime)),
            'search' => $search
        ];
        return ['status' => 'success', 'data' => $data];
    }
    //删除评论
    public static function delComment($commentId) {
        if (!$commentId) {
            return ['status' => 'failed', 'message' => '请求数据不完整！'];
        }
        Comment::where('id', $commentId)->update(['isdel' => '1','adminid'=>Auth::guard('adminweb')->id()]);
        return ['status' => 'success', 'message'=>'删除成功'];
    }
    //处理举报，删除，不处理
    public static function handleReportStatus($type,$commentId) {
        if (!$commentId || !$type) {
            return ['status' => 'failed', 'message' => '请求数据不完整！'];
        }
        if($type == '1') {
            //不处理
            Comment::where('id', $commentId)->update(['reportstatus' => '2','adminid' => Auth::guard('adminweb')->id()]);
        } elseif($type == '2') {
            //删除
            $comment = Comment::where('id', $commentId)->first();
            if(!$comment) {
                return ['status'=>'failed', 'message'=>'获取信息失败'];
            }
            $comment->reportstatus = 3;
            $comment->isdel = 1;
            $comment->adminid = Auth::guard('adminweb')->id();
            $comment->save();
            //shop表
            $shopId = $comment->shopid;
            $score = $comment->score;
            $shop = Shop::where('id', $shopId)->first();
            if($shop) {
                if($shop->commentnumber != 0) {
                    Shop::where('id', $shopId)->decrement('commentnumber', 1);
                    Shop::where('id', $shopId)->decrement('commentscore', $score);
                    $shop->commentmark = round($shop->commentscore / $shop->commentnumber, 2);
                    $shop->save();
                }
            }
        } else {
            return ['status'=>'success', 'message'=>'类型不对'];
        }
        return ['status' => 'success', 'message' => '修改成功'];
    }
    //商家回评
    public static function shopCommentList($cityId, $startTime, $lastTime, $search = '', $page=1, $length = 50) {
        if(!$cityId) {
            return ['status'=>'failed', 'message'=>'参数错误！'];
        }
        if (!$startTime || !$lastTime) {
            $startTime = '2016-01-01 00:00:00';
            $lastTime = date('Y-m-d H:i:s');
        }
        $skip = ($page - 1) * $length;
        if ($search == '') {
            $lists = Comment::where('comments.created_at', '>', $startTime)
                ->where('comments.created_at', '<', $lastTime)
                ->where('comments.replycontent', '!=', '')
                ->where('comments.isdel', '0')
                ->join('users', 'users.id', '=', 'comments.userid')
                ->join('shops', 'shops.id', '=', 'comments.shopid')
                ->where('shops.cityid', $cityId)
                ->select('comments.id as commentid', 'comments.ordernumber', 'users.mobile', 'users.nickname', 'shops.name as shopname', 'comments.replytime', 'comments.replycontent')
                ->orderBy('comments.created_at', 'desc')
                ->skip($skip)->take($length)->get();
            $total = Comment::where('comments.created_at', '>', $startTime)
                ->where('comments.created_at', '<', $lastTime)
                ->where('comments.replycontent', '!=', '')
                ->where('comments.isdel', '0')
                ->join('shops', 'shops.id', '=', 'comments.shopid')
                ->where('shops.cityid', $cityId)
                ->count();
        } else {
            $GLOBALS['search'] = $search;
            $lists = Comment::where('comments.created_at', '>', $startTime)
                ->where('comments.created_at', '<', $lastTime)
                ->where('comments.replycontent', '!=', '')
                ->where('comments.isdel', '0')
                ->join('users', 'users.id', '=', 'comments.userid')
                ->join('shops', 'shops.id', '=', 'comments.shopid')
                ->where('shops.cityid', $cityId)
                ->where(function ($query) {
                    $query->orWhere('comments.ordernumber', $GLOBALS['search'])
                        ->orWhere('users.mobile', $GLOBALS['search'])
                        ->orWhere('users.nickname', 'like', '%'.$GLOBALS['search'].'%')
                        ->orWhere('shops.name', 'like', '%'.$GLOBALS['search'].'%');
                })
                ->select('comments.id as commentid', 'comments.ordernumber', 'users.mobile', 'users.nickname', 'shops.name as shopname', 'comments.replytime', 'comments.replycontent')
                ->orderBy('comments.created_at', 'desc')
                ->skip($skip)->take($length)->get();
            $total = Comment::where('comments.created_at', '>', $startTime)
                ->where('comments.created_at', '<', $lastTime)
                ->where('comments.replycontent', '!=', '')
                ->where('comments.isdel', '0')
                ->join('users', 'users.id', '=', 'comments.userid')
                ->join('shops', 'shops.id', '=', 'comments.shopid')
                ->where('shops.cityid', $cityId)
                ->where(function ($query) {
                    $query->orWhere('comments.ordernumber', $GLOBALS['search'])
                        ->orWhere('users.mobile', $GLOBALS['search'])
                        ->orWhere('users.nickname', 'like', '%'.$GLOBALS['search'].'%')
                        ->orWhere('shops.name', 'like', '%'.$GLOBALS['search'].'%');
                })
                ->count();
        }
        $lastPage = ceil($total / $length);
        $data = [
            'cityid' => $cityId,
            'lists' => $lists,
            'total' => $total,
            'page' => $page,
            'lastpage' => $lastPage,
            'perpage' => $length,
            'starttime' => date('Y-m-d', strtotime($startTime)),
            'lasttime' => date('Y-m-d', strtotime($lastTime)),
            'search' => $search
        ];
        return ['status' => 'success', 'data' => $data];
    }
    //删除商家评论
    public static function delShopComment($commentId) {
        if (!$commentId) {
            return ['status' => 'failed', 'message' => '请求数据不完整！'];
        }
        Comment::where('id', $commentId)->update(['replycontent' => '','replytime'=>'','adminid'=>Auth::guard('adminweb')->id()]);
        return ['status' => 'success', 'message' => '删除成功'];
    }
    //退款列表, 这里是充值退款
    public static function rechargeRefundList($startTime, $lastTime, $search='', $perpage=50) {
        if(!$startTime||!$lastTime) {
            $startTime = '2016-07-01';
            $lastTime = date('Y-m-d');
        }
        if($search) {
            $GLOBALS['search'] = $search;
            $data = Refund::where('refunds.created_at', '>=', $startTime)->where('refunds.created_at', '<=', $lastTime)
                ->join('users', 'refunds.userid', '=', 'users.id')
                ->where(function($query) {
                    $query->orwhere('users.nickname', 'like', '%'.$GLOBALS['search'].'%')
                        ->orwhere('users.mobile', $GLOBALS['search']);
                })
                ->orderBy('refunds.created_at', 'desc')
                ->select('refunds.created_at', 'users.nickname', 'users.mobile', 'refunds.money', 'refunds.fee', 'refunds.actualmoney', 'refunds.startbalance', 'refunds.lastbalance','refunds.note')
                ->paginate($perpage);
        } else {
            $data = Refund::where('refunds.created_at', '>=', $startTime)->where('refunds.created_at', '<=', $lastTime)
                ->join('users', 'refunds.userid', '=', 'users.id')
                ->orderBy('refunds.created_at', 'desc')
                ->select('refunds.created_at', 'users.nickname', 'users.mobile', 'refunds.money', 'refunds.fee', 'refunds.actualmoney', 'refunds.startbalance', 'refunds.lastbalance','refunds.note')
                ->paginate($perpage);
        }
        return ['status'=>'success', 'data'=>$data];
    }
    //导出充值退款记录
    public static function rechargeRefundListExport($cityId,$startTime, $lastTime, $search){
        if(!$cityId){
            return ['status'=>'failed','message'=>'参数错误'];
        }
        if(!$startTime||!$lastTime) {
            $startTime = '2016-07-01';
            $lastTime = date('Y-m-d');
        }
        if($search) {
            $GLOBALS['search'] = $search;
            $data = Refund::where('refunds.created_at', '>=', $startTime)->where('refunds.created_at', '<=', $lastTime)
                ->join('users', 'refunds.userid', '=', 'users.id')
                ->where(function($query) {
                    $query->orwhere('users.nickname', 'like', '%'.$GLOBALS['search'].'%')
                        ->orwhere('users.mobile', $GLOBALS['search']);
                })
                ->orderBy('refunds.created_at', 'desc')
                ->select('refunds.created_at', 'users.nickname', 'users.mobile', 'refunds.money', 'refunds.fee', 'refunds.actualmoney', 'refunds.startbalance', 'refunds.lastbalance','refunds.note')
                ->get();
        } else {
            $data = Refund::where('refunds.created_at', '>=', $startTime)->where('refunds.created_at', '<=', $lastTime)
                ->join('users', 'refunds.userid', '=', 'users.id')
                ->orderBy('refunds.created_at', 'desc')
                ->select('refunds.created_at', 'users.nickname', 'users.mobile', 'refunds.money', 'refunds.fee', 'refunds.actualmoney', 'refunds.startbalance', 'refunds.lastbalance','refunds.note')
                ->get();
        }
        return ['status'=>'success', 'data'=>$data];
    }
    //充值退款统计数据
    public static function getRechargeRefundStatistic($startTime, $lastTime, $search='') {
        if(!$startTime||!$lastTime) {
            $startTime = '2016-07-01';
            $lastTime = date('Y-m-d');
        }
        if($search) {
            $GLOBALS['search'] = $search;
            $count = Refund::where('refunds.created_at', '>=', $startTime)->where('refunds.created_at', '<=', $lastTime)
                ->join('users', 'refunds.userid', '=', 'users.id')
                ->where(function($query) {
                    $query->orwhere('users.nickname', 'like', '%'.$GLOBALS['search'].'%')
                        ->orwhere('users.mobile', $GLOBALS['search']);
                })
                ->get()->count();
            $moneyTotal = Refund::where('refunds.created_at', '>=', $startTime)->where('refunds.created_at', '<=', $lastTime)
                ->join('users', 'refunds.userid', '=', 'users.id')
                ->where(function($query) {
                    $query->orwhere('users.nickname', 'like', '%'.$GLOBALS['search'].'%')
                        ->orwhere('users.mobile', $GLOBALS['search']);
                })
                ->sum('money');
            $feeTotal = Refund::where('refunds.created_at', '>=', $startTime)->where('refunds.created_at', '<=', $lastTime)
                ->join('users', 'refunds.userid', '=', 'users.id')
                ->where(function($query) {
                    $query->orwhere('users.nickname', 'like', '%'.$GLOBALS['search'].'%')
                        ->orwhere('users.mobile', $GLOBALS['search']);
                })
                ->sum('fee');
            $actualMoneyTotal = Refund::where('refunds.created_at', '>=', $startTime)->where('refunds.created_at', '<=', $lastTime)
                ->join('users', 'refunds.userid', '=', 'users.id')
                ->where(function($query) {
                    $query->orwhere('users.nickname', 'like', '%'.$GLOBALS['search'].'%')
                        ->orwhere('users.mobile', $GLOBALS['search']);
                })
                ->sum('actualmoney');
        } else {
            $count = Refund::where('created_at', '>=', $startTime)->where('created_at', '<=', $lastTime)
                ->get()->count();
            $moneyTotal = Refund::where('created_at', '>=', $startTime)->where('created_at', '<=', $lastTime)
                ->sum('money');
            $feeTotal = Refund::where('created_at', '>=', $startTime)->where('created_at', '<=', $lastTime)
                ->sum('fee');
            $actualMoneyTotal = Refund::where('created_at', '>=', $startTime)->where('created_at', '<=', $lastTime)
                ->sum('actualmoney');
        }
        $data = [
            'count' => $count,
            'moneytotal' => $moneyTotal,
            'feetotal' => $feeTotal,
            'actualmoneytotal' => $actualMoneyTotal
        ];
        return ['status'=>'success', 'data'=>$data];
    }
    //订单退款列表， 这里是订单退款
    public static function orderRefundList($cityId, $startTime, $lastTime, $search='') {
        if(!$cityId||!$startTime||!$lastTime) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        if($search) {
            $GLOBALS['search'] = $search;
            $data = OrderRefund::where('order_refunds.status', 1)
                ->join('orders', 'order_refunds.orderid', '=', 'orders.id')
                ->join('users', 'order_refunds.userid', '=', 'users.id')
                ->where('orders.cityid', $cityId)
                ->where('order_refunds.created_at', '>=', $startTime)
                ->where('order_refunds.created_at', '<=', $lastTime)
                ->where(function($query) {
                    $query->orwhere('users.mobile', $GLOBALS['search'])->orwhere('users.nickname', 'like', '%'.$GLOBALS['search'].'%')
                    ->orwhere('order_refunds.ordernumber', $GLOBALS['search']);
                })
                ->orderBy('order_refunds.created_at', 'desc')
                ->select('order_refunds.created_at', 'order_refunds.ordernumber', 'order_refunds.orderid', 'users.mobile', 'users.nickname', 'order_refunds.money', 'order_refunds.redenvelopeinfo', 'order_refunds.remark')
                ->get();
        } else {
            $data = OrderRefund::where('order_refunds.status', 1)
                ->join('orders', 'order_refunds.orderid', '=', 'orders.id')
                ->join('users', 'order_refunds.userid', '=', 'users.id')
                ->where('orders.cityid', $cityId)
                ->where('order_refunds.created_at', '>=', $startTime)
                ->where('order_refunds.created_at', '<=', $lastTime)
                ->orderBy('order_refunds.created_at', 'desc')
                ->select('order_refunds.created_at', 'order_refunds.ordernumber', 'order_refunds.orderid', 'users.mobile', 'users.nickname', 'order_refunds.money', 'order_refunds.redenvelopeinfo', 'order_refunds.remark')
                ->get();
        }
        return ['status'=>'success', 'data'=>$data];
    }

    //导出订单管理中订单消费记录
    public static function orderRefundListExport($cityId, $startTime, $lastTime, $search=''){
        if(!$cityId||!$startTime||!$lastTime) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        if($search) {
            $GLOBALS['search'] = $search;
            $data = OrderRefund::where('order_refunds.status', 1)
                ->join('orders', 'order_refunds.orderid', '=', 'orders.id')
                ->join('users', 'order_refunds.userid', '=', 'users.id')
                ->where('orders.cityid', $cityId)
                ->where('order_refunds.created_at', '>=', $startTime)
                ->where('order_refunds.created_at', '<=', $lastTime)
                ->where(function($query) {
                    $query->orwhere('users.mobile', $GLOBALS['search'])->orwhere('users.nickname', 'like', '%'.$GLOBALS['search'].'%')
                        ->orwhere('order_refunds.ordernumber', $GLOBALS['search']);
                })
                ->orderBy('order_refunds.created_at', 'desc')
                ->select('order_refunds.created_at', 'order_refunds.ordernumber', 'order_refunds.orderid', 'users.mobile', 'users.nickname', 'order_refunds.money', 'order_refunds.redenvelopeinfo', 'order_refunds.remark')
                ->get();
        } else {
            $data = OrderRefund::where('order_refunds.status', 1)
                ->join('orders', 'order_refunds.orderid', '=', 'orders.id')
                ->join('users', 'order_refunds.userid', '=', 'users.id')
                ->where('orders.cityid', $cityId)
                ->where('order_refunds.created_at', '>=', $startTime)
                ->where('order_refunds.created_at', '<=', $lastTime)
                ->orderBy('order_refunds.created_at', 'desc')
                ->select('order_refunds.created_at', 'order_refunds.ordernumber', 'order_refunds.orderid', 'users.mobile', 'users.nickname', 'order_refunds.money', 'order_refunds.redenvelopeinfo', 'order_refunds.remark')
                ->get();
        }
        return ['status'=>'success', 'data'=>$data];
    }
    //订单退款列表中的统计数据
    public static function getOrderRefundStatistic($cityId, $startTime, $lastTime, $search='') {
        if(!$cityId||!$startTime||!$lastTime) {
            return [
                'orderrefundcount' => 0,
                'orderrefundsum' => 0
            ];
        }
        if($search) {
            $GLOBALS['search'] = $search;
            $orderRefundCount = OrderRefund::where('order_refunds.status', 1)
                ->join('orders', 'order_refunds.orderid', '=', 'orders.id')
                ->join('users', 'order_refunds.userid', '=', 'users.id')
                ->where('orders.cityid', $cityId)
                ->where(function($query) {
                    $query->orwhere('users.mobile', $GLOBALS['search'])->orwhere('users.nickname', 'like', '%'.$GLOBALS['search'].'%')
                        ->orwhere('order_refunds.ordernumber', $GLOBALS['search']);
                })
                ->get()->count();
            $orderRefundSum = OrderRefund::where('order_refunds.status', 1)
                ->join('orders', 'order_refunds.orderid', '=', 'orders.id')
                ->join('users', 'order_refunds.userid', '=', 'users.id')
                ->where('orders.cityid', $cityId)
                ->where(function($query) {
                    $query->orwhere('users.mobile', $GLOBALS['search'])->orwhere('users.nickname', 'like', '%'.$GLOBALS['search'].'%')
                        ->orwhere('order_refunds.ordernumber', $GLOBALS['search']);
                })
                ->sum('order_refunds.money');
        } else {
            $orderRefundCount = OrderRefund::where('order_refunds.status', 1)
                ->join('orders', 'order_refunds.orderid', '=', 'orders.id')
                ->join('users', 'order_refunds.userid', '=', 'users.id')
                ->where('orders.cityid', $cityId)
                ->orderBy('order_refunds.created_at', 'desc')
                ->get()->count();
            $orderRefundSum = OrderRefund::where('order_refunds.status', 1)
                ->join('orders', 'order_refunds.orderid', '=', 'orders.id')
                ->join('users', 'order_refunds.userid', '=', 'users.id')
                ->where('orders.cityid', $cityId)
                ->sum('order_refunds.money');
        }
        return [
            'orderrefundcount' => $orderRefundCount,
            'orderrefundsum' => $orderRefundSum
        ];
    }
    //通过userid和ordernumber获取退款金额
    public static function getMoneyAfterOrderRefund($userId, $orderNumber) {
        if(!$userId || !$orderNumber) {
            return ['status'=>'failed', 'message'=>'参数错误'];
        }
        $order = Order::where('userid', $userId)->where('ordernumber', $orderNumber)->first();
        if(!$order) {
            return ['status'=>'failed', 'message'=>'退款失败，稍后再试'];
        }
        $redEnvelopeRemark = '';
        //是否派送过红包
        if($order->bonusid) {
            //已派送红包
            if(Order::where('redenvelopeid', $order->bonusid)->get()->count()>0) {
                //使用了已派送的红包, 退款金额=实付金额 - 已派发红包金额
                $refundMoney = $order->actualamount - $order->bonus;
            } else {
                //未使用已派送的红包
                $refundMoney = $order->actualamount;
                $redEnvelopeRemark = "本次派送的红包" . $order->bonus . '更改状态为"已失效"';
            }
        } else {
            //未派送红包
            $refundMoney = $order->actualamount;

        }
        //修改已使用的红包状态
        if($order->redenvelopeid) {
            //该订单买单时使用过红包
            $redEnvelopeInfo = RedEnvelope::where('id', $order->redenvelopeid)->first();
            if($redEnvelopeInfo) {
                //修改红包状态 0表示未使用，1已使用，2已过期，3已失效
                if($redEnvelopeInfo->endtime > date('Y-m-d H:i:s')) {
                    if($redEnvelopeRemark) {
                        $redEnvelopeRemark .= ",本次订单使用的红包" . $redEnvelopeInfo->amount . '更换状态为"未使用"';
                    } else {
                        $redEnvelopeRemark = "本次订单使用的红包" . $redEnvelopeInfo->amount . '更换状态为"未使用"';
                    }
                } else {
                    if($redEnvelopeRemark) {
                        $redEnvelopeRemark .= ",本次订单使用的红包" . $redEnvelopeInfo->amount . '更换状态为"已失效"';
                    } else {
                        $redEnvelopeRemark = "本次订单使用的红包" . $redEnvelopeInfo->amount . '更换状态为"已失效"';
                    }
                }
            }
        }
        $data = [
            'refundmoney' => $refundMoney,
            'redenvelopeinfo' => $redEnvelopeRemark,
        ];
        return ['status'=>'success', 'data'=>$data];
    }
}